package com.weifly.weistock.record.option;

import com.weifly.weistock.core.common.StockException;
import com.weifly.weistock.record.option.domain.MergeRecordResult;
import com.weifly.weistock.record.option.domain.OptionRecordDto;
import com.weifly.weistock.record.option.domain.OptionYearDto;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

/**
 * 期权工具方法
 *
 * @author weifly
 * @since 2019/11/18
 */
public class OptionUtils {

    public static void checkEmpty(Object value, String message){
        if(value==null){
            throw new StockException(message);
        }
        if(value instanceof String){
            if(StringUtils.isBlank((String)value)){
                throw new StockException(message);
            }
        }else if(value instanceof List){
            if(((List)value).isEmpty()){
                throw new StockException(message);
            }
        }
    }

    public static void copyRecordProp(OptionRecordDto source, OptionRecordDto target) {
        target.setDate(source.getDate());
        target.setContractName(source.getContractName());
        target.setContractCode(source.getContractCode());
        target.setStockName(source.getStockName());
        target.setStockCode(source.getStockCode());
        target.setBusinessName(source.getBusinessName());
        target.setOperationType(source.getOperationType());
        target.setHoldPosition(source.getHoldPosition());
        target.setTradePrice(source.getTradePrice());
        target.setTradeNumber(source.getTradeNumber());
        target.setTradeAmount(source.getTradeAmount());
        target.setFeeService(source.getFeeService());
        target.setFeeStamp(source.getFeeStamp());
        target.setFeeTransfer(source.getFeeTransfer());
        target.setFeeExtra(source.getFeeExtra());
        target.setFeeClear(source.getFeeClear());
        target.setClearAmount(source.getClearAmount());
        target.setAfterAmount(source.getAfterAmount());
        target.setAfterNumber(source.getAfterNumber());
        target.setEntrustCode(source.getEntrustCode());
        target.setTime(source.getTime());
    }

    public static void addRecordToYear(OptionYearDto yearDto, OptionRecordDto recordDto, MergeRecordResult result){
        String key = recordDto.getDate() + " - " + recordDto.getEntrustCode();
        OptionRecordDto targetDto = yearDto.getRecordMap().get(key); // 同一天，同一个委托 的记录
        if(targetDto==null){
            yearDto.getRecordList().add(recordDto);
            yearDto.getRecordMap().put(key, recordDto);
            if(result!=null){
                result.setInsert(result.getInsert()+1);
            }
        }else{
            OptionUtils.copyRecordProp(recordDto, targetDto);
            if(result!=null){
                result.setUpdate(result.getUpdate()+1);
            }
        }
    }
}
